package com.durian.base.frame.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.durian.base.frame.IActivityStack;
import com.durian.base.frame.dialog.CustomProgressDialog;
import com.durian.base.frame.dialog.ILoadingDialog;
import com.durian.base.frame.easyrouter.EasyRouter;
import com.durian.base.frame.fragment.SupportFragment;
import com.durian.base.frame.titlebar.ISuperTitleBar;
import com.durian.base.frame.titlebar.TitleBarSupport;
import com.durian.base.utils.KeyBoardUtils;
import com.durian.base.utils.ToastUtils;
import com.durian.base.utils.ToolUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;


/**
 * 说明：ActivityFrame
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-21 17:45
 * <p/>
 * 版本：version 1.0
 */
public abstract class ActivityFrame extends AppCompatActivity implements IActivity, IActivityStack {

    private   SupportFragment currentFragment;
    private   boolean         isInitStart = false;
    protected ILoadingDialog  mLoadingDialog;
    private   LoadSir.Builder mLoadBuilder;
    protected LoadService     mLoadService;
    protected ISuperTitleBar  mSuperTitleBar;
    private   TitleBarSupport mTitleBarSupport;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (getIntent() != null) {
            getIntentData(getIntent());
        }
        setViewBefor();
        View rootView = bindRootView();
        if (rootView != null) {
            setContentView(rootView);
        } else if (getRootViewResID() > 0) {
            setContentView(getRootViewResID());
        }
        onInitCreate(bundle);
    }

    @Override
    public void onInitCreate(Bundle bundle) {
        if (isOpenARouterInject()) {
            EasyRouter.inject(this);
        }
        if (isOpenLoadSir()) {
            mLoadBuilder = createLoadSirBuilder();
            if (mLoadBuilder != null) {
                addLoadSirCallBack(mLoadBuilder);
                LoadSir loadSir = mLoadBuilder.build();
                mLoadService = loadSir.register(setLoadSirTarget(), setOnReloadListener());
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!isInitStart) {
            onInitStart();
            isInitStart = true;
        }
    }

    @Override
    public void getIntentData(Intent intent) {

    }

    protected ActivityFrame getActivity() {
        return this;
    }


    /**
     * 说明:设置界面之前调用
     *
     * @return
     */
    protected void setViewBefor() {
    }

    /**
     * 说明：设置界面布局资源
     *
     * @return
     */
    protected int getRootViewResID() {
        return 0;
    }

    /**
     * 说明：设置界面布局
     *
     * @return
     */
    protected View bindRootView() {
        return null;
    }

    /**
     * 说明：是否加入Activity管理栈，默认不加入
     *
     * @return
     */
    @Override
    public boolean addActivityStack() {
        return true;
    }


    /**
     * 打开arouter注入，默认打开
     *
     * @return
     */
    protected boolean isOpenARouterInject() {
        return false;
    }

    /**
     * 说明：用Fragment替换视图
     *
     * @param srcView 被替换视图
     * @param targetFragment 用来替换的Fragment
     */
    public void changeFragment(int srcView, SupportFragment targetFragment) {
        if (targetFragment.equals(currentFragment)) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!targetFragment.isAdded()) {
            transaction.add(srcView, targetFragment, targetFragment.getClass().getName());
        }
        if (targetFragment.isHidden()) {
            transaction.show(targetFragment);
        }
        if (currentFragment != null && currentFragment.isVisible()) {
            transaction.hide(currentFragment);
        }
        currentFragment = targetFragment;
        transaction.commitNow();
    }

    /**
     * 说明：用Fragment替换视图
     *
     * @param srcView 被替换视图
     * @param targetFragment 用来替换的Fragment
     */
    public void replaceFragment(int srcView, SupportFragment targetFragment) {
        if (targetFragment.equals(currentFragment)) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(srcView, targetFragment).commitNow();
        currentFragment = targetFragment;
    }


    /*************** UI *************/

    public void showLoading() {
        showLoading(true);
    }

    public void showLoading(boolean isCancel) {
        dismissLoading();
        mLoadingDialog = setLoadingDialog();
        if (mLoadingDialog != null) {
            mLoadingDialog.setCancel(isCancel).showDialog(getSupportFragmentManager());
        }
    }

    public void dismissLoading() {
        if (mLoadingDialog != null && mLoadingDialog instanceof CustomProgressDialog) {
            CustomProgressDialog progressDialog = (CustomProgressDialog) mLoadingDialog;
            if (progressDialog.getOwnerActivity() != null && !progressDialog.getOwnerActivity().isFinishing()) {
                progressDialog.dismiss();
            }
        }
        mLoadingDialog = null;
    }

    public ILoadingDialog setLoadingDialog() {
        if (ToolUtils.isNotFinish(this)) {
            CustomProgressDialog progressDialog = new CustomProgressDialog(this);
            progressDialog.setOwnerActivity(this);
            return progressDialog;
        } else {
            return null;
        }
    }

    public void shortToast(int res) {
        ToastUtils.get().shortToast(res);
    }

    public void shortToast(String res) {
        ToastUtils.get().shortToast(res);
    }

    public void longToast(String res) {
        ToastUtils.get().longToast(res);
    }

    public void longToast(int res) {
        ToastUtils.get().longToast(res);
    }

    public void cancelToast() {
        ToastUtils.get().cancelToast();
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        mSuperTitleBar = bindSuperTitleBar();
        if (mSuperTitleBar == null) {
            super.setContentView(layoutResID);
        }else {
            mTitleBarSupport = new TitleBarSupport();
            mTitleBarSupport.bind(this, mSuperTitleBar, layoutResID);
        }
    }

    @Override
    public void setContentView(View view) {
        if (mTitleBarSupport != null){
            super.setContentView(view);
        }else {
            if (mSuperTitleBar == null) {
                mSuperTitleBar = bindSuperTitleBar();
            }
            if (mSuperTitleBar != null) {
                mTitleBarSupport = new TitleBarSupport();
                mTitleBarSupport.bind(this, mSuperTitleBar, view);
            } else {
                super.setContentView(view);
            }
        }
    }

    /*************** UI *************/

    @Override
    protected void onPause() {
        super.onPause();
        dismissLoading();
        KeyBoardUtils.hiddenSoftInput(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSuperTitleBar != null){
            mSuperTitleBar.unBind();
        }
        if (mTitleBarSupport != null){
            mTitleBarSupport.unBind();
        }
        mLoadService = null;
        mLoadBuilder = null;
    }


    /****************************** 菜单设置 *********************************/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (setMenu() != 0) {
            getMenuInflater().inflate(setMenu(), menu);
            return true;
        } else {
            return super.onCreateOptionsMenu(menu);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (setMenu() != 0) {
            onMenuItemClick(item.getItemId());
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    /**
     * 说明：菜单点击处理
     *
     * @param id
     */
    protected void onMenuItemClick(int id) {
    }

    /**
     * 说明：设置菜单选项
     *
     * @return
     */
    protected int setMenu() {
        return 0;
    }

    /****************************** 菜单设置 *********************************/


    /***************   配置loadsir  ****************/

    protected LoadSir.Builder createLoadSirBuilder() {
        return new LoadSir.Builder();
    }

    /**
     * 添加页面状态配置
     *
     * @param builder
     */
    public void addLoadSirCallBack(LoadSir.Builder builder) {
    }

    public boolean isOpenLoadSir() {
        return false;
    }

    public Object setLoadSirTarget() {
        return this;
    }

    public Callback.OnReloadListener setOnReloadListener() {
        return null;
    }

    /***************   配置loadsir  ****************/

    protected ISuperTitleBar bindSuperTitleBar() {
        return null;
    }
}
